home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / et / et-2_2.lha / et2.2 / src / Text.h < prev    next >
C/C++ Source or Header  |  1990-12-04  |  7KB  |  220 lines

  1. #ifndef Text_First
  2. #ifdef __GNUG__
  3. #pragma once
  4. #endif
  5. #define Text_First
  6.  
  7. #include "Object.h"
  8. #include "String.h"
  9. #include "Font.h"
  10. #include "Port.h"
  11.  
  12. //---- descriptor of a text line --------------------------------------------
  13.  
  14. struct LineDesc {
  15.     int lnAscent;
  16.     int lnHeight;
  17.     LineDesc(int b = 0, int h = 0);
  18.     void Reset();
  19.     ostream& DisplayOn (ostream&s);
  20.  
  21.     bool IsEqual(LineDesc l)
  22.     { return (bool) (lnAscent == l.lnAscent && lnHeight == l.lnHeight); }
  23.  
  24.     void Max(FontPtr f);
  25.     void Max(LineDesc ld);
  26.     void Max(int ascent, int height);
  27.  
  28.     void FromFont(FontPtr f)
  29.     { lnAscent = f->Ascender(); lnHeight = f->Spacing(); }
  30. };
  31.  
  32. //---- abstract class Text ----------------------------------------------
  33.  
  34. const int cEOT = -1,
  35.       cTabw = 40;
  36.  
  37. inline bool CheckRange (int max, int from, int to)
  38. {
  39.     return (to > max || from < 0 || from > to) ? FALSE: TRUE;
  40. }
  41.  
  42. //---- change protocol -------------------------------------------------
  43.  
  44. // text is an abstract class defining methods to manage a buffer for holding 
  45. // text. Together with the text there is a list of robust pointers (called marks)
  46. // into the text. Dependents of the Text classes are notified by 
  47. // the observer mechanism
  48. // and receive a change record as described below as argument
  49.  
  50. enum eTextChanges { 
  51.     eTextChangedRange, 
  52.     eTextDeleted,
  53.     eTextReplaced       // from,to=> replaced range, size=>size of inserted text
  54. };
  55.  
  56. struct TextChanges {
  57.     int from;
  58.     int to;
  59.     int size;
  60.     TextChanges *operator()(int f, int t, int s= 0)
  61.     { from= f; to= t; size= s; return this; }
  62. };
  63.  
  64. //---- Text -----------------------------------------------------------------
  65.  
  66. typedef class Text *TextPtr;
  67.  
  68. class Text: public Object { 
  69.     class MarkList *marks;
  70.     class Collection *observers;  
  71.     int tabWidth; 
  72.                                    
  73. protected:
  74.     Font *font;                        
  75.     Ink *ink;                        
  76.  
  77.     Text(); 
  78.     virtual void CalcIws (int width, int from, int *to, int *addSpace, 
  79.                         int *longBlanks, int *numTabs);
  80.     // calculate the interword spacing for a justified text line
  81.     // 'to' is set to the position of the last non blank character
  82.     virtual void SetFStringVL(char *fmt, va_list ap); 
  83.     virtual int GrowBy(int desiredSize);
  84.     
  85.     // optimized observing 
  86.     class Collection *MakeObserverColl();
  87.     class Collection *GetObservers();
  88.     void DestroyObserverColl();
  89.     void SetObserverColl(Collection *);
  90. public:    
  91.     MetaDef(Text);
  92.     void InitNew();
  93.  
  94.     ~Text();
  95.  
  96.     //---- editing
  97.     virtual void Cut(int from,int to);   
  98.     virtual void Paste(TextPtr t,int from,int to); 
  99.     virtual void Insert(byte c, int from,int to);
  100.     virtual void Append(byte c); 
  101.     virtual void Copy(Text* save,int from, int to); // abstract
  102.  
  103.     //---- converting
  104.     virtual void ReplaceWithStr(byte *str,int len = -1); 
  105.     virtual void CopyInStr(byte *str,int strSize,int from, int to); 
  106.     char *AsString();
  107.     int  AsInt();
  108.     float AsFloat(); 
  109.  
  110.     //---- accessing
  111.     void SetFString(char *fmt, ...);
  112.     virtual byte *GetTextAt(int from, int to);  // obsolete
  113.     virtual Text *GetScratchText(int size); 
  114.     virtual Text *Save(int from, int to); // abstract
  115.     // allocate new text object and copy the given range
  116.     virtual int SetTabWidth(int); // returns old tabWidth
  117.     virtual int Tabulate(int cx); 
  118.     // return the width of a tabulator based on cTabw
  119.     virtual void Empty(int initSize = 0);  //abstract
  120.     virtual byte& operator[](int i); //abstract      
  121.     virtual int Size(); // abstract
  122.     virtual bool IsEmpty();
  123.     virtual int Search(class RegularExp *rex,int *nMatched, int start = 0, 
  124.          int range = cMaxInt, bool forward = TRUE);
  125.         // search rex and return position of match (-1 == no match)
  126.  
  127.     //---- fonts and metric
  128.     virtual void SetFont(FontPtr fd); 
  129.     virtual FontPtr GetFont(int at = 0);
  130.     virtual void SetInk(Ink*);
  131.     virtual Ink *GetInk(int at = 0); 
  132.     virtual void ResetCurrentStyle();
  133.     
  134.     virtual int TextWidth(int from, int to); // abstract
  135.     virtual void DrawText(int from, int to, Rectangle clip); 
  136.     virtual void DrawTextJust (int from, int to, int w, Point start, Rectangle clip); 
  137.     // draw the text in the given range at the current text position,abstract
  138.     virtual void JustifiedMap(int from, int to, int w, int stopAt, int posX, 
  139.                                int *charPos, int *x);
  140.     virtual void Map(int from, int to, int stopAt, int posX, 
  141.                                int *charPos, int *x);
  142.     FontPtr SetDrawingState(int at);
  143.     
  144.     //---- iterator
  145.     virtual class TextIter *MakeIterator(int from=0, int to= cMaxInt); //abstract
  146.  
  147.     //---- utilities
  148.     virtual void GetWordBoundaries(int at,int *start,int *end);
  149.     virtual void GetParagraphBoundaries(int at,int *start,int *end);
  150.  
  151.     //---- Marks
  152.     virtual void AddMark(class Mark *);
  153.     virtual Mark *RemoveMark(class Mark *);
  154.     virtual class Iterator *GetMarkIter(); 
  155.     virtual class MarkList *GetMarkList();    // ????   
  156.  
  157.     //---- input/output
  158.     virtual ostream& PrintOnAsPureText(ostream &s);
  159.     virtual istream& ReadFromAsPureText(istream &s, long sizeHint= -1);
  160.  
  161.     //---- inspecting 
  162.     void InspectorId(char *b, int s);
  163. };
  164.  
  165. //---- abstract class TextIter ----------------------------------------------
  166.  
  167. class TextIter : public Root {
  168. protected:
  169.     int ce;
  170.     int upto;
  171.     Text *ct;
  172.     int unget;
  173. public:
  174.     TextIter(Text *s,int from = 0,int to = cMaxInt);        
  175.     virtual int operator()();      // abstract
  176.     virtual int operator()(int *width, LineDesc* l = 0); // abstract                                          
  177.     virtual void Reset(Text *s,int from = 0, int to = cMaxInt);        
  178.     virtual int Line(LineDesc* l = 0); // return end of next line, abstract
  179.     virtual int Token(int *width, LineDesc* l = 0); // return next token and width, abstract
  180.     virtual int GetPos();
  181.     virtual Font *FontAt(int);
  182.     virtual int GetLastPos();                 // get last position
  183.     virtual void SetPos(int newPos);
  184.     virtual int Unget();                     // unget last token
  185. };
  186.  
  187. class AutoTextIter { 
  188.     TextIter *ti;
  189. public:
  190.     AutoTextIter(Text *t, int from= 0, int to= cMaxInt);
  191.     AutoTextIter(TextIter *tip)
  192.     { ti= tip; }
  193.     ~AutoTextIter()
  194.     { if (ti) delete ti; }
  195.     int operator()()
  196.     { return (*ti)(); }
  197.     void Reset(Text *s,int from= 0, int to= cMaxInt)
  198.     { ti->Reset(s,from,to); }        
  199.     int operator()(int *width, LineDesc* l= 0) 
  200.     { return (*ti)(width,l); }
  201.     int Line(LineDesc* l= 0)
  202.     { return ti->Line(l); } 
  203.     int Token(int *width, LineDesc* l= 0)
  204.     { return ti->Token(width,l); }
  205.     int GetPos()
  206.     { return ti->GetPos(); }
  207.     Font *FontAt(int i)
  208.     { return ti->FontAt(i); }
  209.     int GetLastPos()  
  210.     { return ti->GetLastPos(); }               
  211.     void SetPos(int newPos)
  212.     { ti->SetPos(newPos); }
  213.     int Unget()
  214.     { return ti->Unget(); }                     
  215. };
  216.  
  217.  
  218. #endif Text_First
  219.  
  220.